define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'volunteer_audit/index' + location.search,
                    add_url: 'volunteer_audit/add',
                    edit_url: 'volunteer_audit/edit',
                    // del_url: 'volunteer_audit/del',
                    multi_url: 'volunteer_audit/multi',
                    import_url: 'volunteer_audit/import',
                    table: 'volunteer_audit',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormVisible: true,
                search:false,
                showColumns:false,
                // showExport:false,
                // commonSearch:false,
                pageSize: 10,
                showToggle: false,
                exportOptions: {
                    title:'aaa',
                    fileName: '開懷圈審核申請_' + Moment().format("YYYY-MM-DD"),  //导出名称
                    ignoreColumn: ['user.avatar','operate'], //不导出字段
                    ignoreRow: [],
                    mso:{
                        // fileFormat:        'xlsx',
                        //修复导出数字不显示为科学计数法
                        onMsoNumberFormat: function (cell, row, col) {
                            return !isNaN($(cell).text())?'\\@':'';
                        }
                    },
                    csvSeparator: "----",//文本分割
                },
                columns: [
                    [
                        {field: 'affiliated_center_text', title: __('Affiliated_center_text'), operate:false, formatter: Table.api.formatter.label},
                        {field: 'area', title: __('所在地區'), operate: false},
                        {field: 'user_id', title: __('User_id'), operate: false},
                        {field: 'user.avatar', title: __('User.avatar'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'user.nickname', title: __('User.nickname'), operate: 'LIKE'},
                        {field: 'name', title: __('Name'), operate: false},
                              {field: 'birthday', title: __('出生日期'), operate: false},
                                    {field: 'id_card_hk', title: __('香港身份證號碼'), operate: false},
                        {field: 'mobile', title: __('香港手機號'), operate: false},

                        {field: 'mobile_china', title: __('內地手機號'), operate: false},
                        {field: 'address', title: __('Address'), operate: false},
                         {field: 'wechat_id', title: __('微信ID'), operate: false},
                          {field: 'service_type', title: __('服務偏好'), operate: false},
                           {field: 'remark', title: __('自我簡介'), operate: false},
                          
                      
                        
                        {field: 'status', title: __('Status'), searchList: {"1":__('Status 1'),"2":__('Status 2'),"3":__('Status 3')}, formatter: Controller.api.formatter.status,events: Controller.api.events.status},
                        {field: 'createtime', title: __('Createtime'), operate:false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter:function(value,row,index){
                                    this.buttons = [
                                        {
                                            name: 'edit',
                                            text: '詳情',
                                            classname: 'btn btn-xs btn-success btn-magic btn-editone',
                                            url: 'volunteer_audit/edit',
                                            // confirm: '确认禁用嗎？',
                                            visible: function (row) {
                                                if ( Config.volunteer_audit_edit ) {
                                                    return true;
                                                }else{
                                                    return false;
                                                }
                                                //返回true时按钮显示,返回false隐藏
                                            },
                                            success: function () {
                                                $(".btn-refresh").trigger("click");
                                            },
                                            error: function () {

                                            },
                                            refresh: true,
                                        },{
                                            name: 'audit',
                                            text: '審核',
                                            classname: 'btn btn-xs btn-danger btn-magic btn-dialog',
                                            url: 'volunteer_audit/audit',
                                            // confirm: '确认禁用嗎？',
                                            visible: function (row) {
                                                if ( Config.volunteer_audit && row.status == 1) {
                                                    return true;
                                                }else{
                                                    return false;
                                                }
                                                //返回true时按钮显示,返回false隐藏
                                            },
                                            success: function () {
                                                $(".btn-refresh").trigger("click");
                                            },
                                            error: function () {

                                            },
                                            refresh: true,
                                        }
                                    ];
                                return Table.api.formatter.operate.call(this,value,row,index);
                            }}
                    ]
                ]
            });

            table.on("pre-body.bs.table",function(){
                var bt=table.data("bootstrap.table");
                if (bt){
                    bt.$toolbar.find(".export").find(".icon-share").text("導出");
                    bt.$toolbar.find(".export").find(".icon-share").removeClass("glyphicon glyphicon-export icon-share");
                }
            });
            
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'volunteer_audit/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'volunteer_audit/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'volunteer_audit/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        audit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {//渲染的方法
                status: function (value, row, index) {
                    if ( row.status == 3 ) {
                        row.status_text = row.status_text+'(查看原因)';
                    }
                    //这里我们直接使用row的数据
                    return '<a class="btn btn-xs btn-browser">' + row.status_text + '</a>';
                }
            },
            events: {//绑定事件的方法
                status: {
                    'click .btn-browser': function (e, value, row, index) {
                        if ( row.status == 3 ) {
                            Layer.alert("<code>" + row.audreason + "</code>", {title: "拒絕原因"});
                        }
                    }
                },
            }
        }
    };
    return Controller;
});